SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE [dbo].[ArchiveReportSelectRange]
    @UserId INT ,
    @StartDate DATETIME ,
    @EndDate DATETIME
AS 
    BEGIN
        SELECT  ArchiveReport.Id ,
                ArchiveReport.UserId ,
                UserInfo.Login ,
                ArchiveReport.TemplateId ,
                WordTemplate.FileName ,
                ArchiveReport.Name ,
                ArchiveReport.DateCreated ,
                ArchiveReport.Deleted
        FROM    ArchiveReport
                LEFT JOIN UserInfo ON ( ArchiveReport.UserId = UserInfo.Id )
                LEFT JOIN WordTemplate ON (ArchiveReport.TemplateId = WordTemplate.Id)
        WHERE   ArchiveReport.Deleted = 0
                AND ArchiveReport.DateCreated BETWEEN @StartDate
                                              AND     @EndDate
                AND ( ( @UserId > 0
                        AND ArchiveReport.UserId = @UserId
                      )
                      OR ( @UserId = 0 )
                    )
        ORDER BY ArchiveReport.DateCreated
    END
GO
